package com.itgy.project.provider;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itgy.project.common.ErrorCode;
import com.itgy.project.exception.BusinessException;
import com.itgy.project.mapper.UserMapper;
import com.itgy.project.model.entity.User;
import com.itgy.project.provider.InnerUserInfo;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@DubboService
public class InnerUserInfoImpl implements InnerUserInfo {

    @Resource
    private UserMapper userMapper;


    @Override
    public Map<String,String> getUserInfoByAccessKey(String accessKey) {
        if (StrUtil.isBlank(accessKey)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getAccessKey,accessKey);
        User user = userMapper.selectOne(lambdaQueryWrapper);
        if(user == null)
            return null;
        Map<String,String> map = new HashMap<>();
        map.put("userId",user.getId().toString());
        map.put("secretKey",user.getSecretKey());
        return map;
    }
}
